package exercise.tute13kengine.version2;

// note the following imports
import kengine.version1.Doc;
import kengine.version1.Query;
import kengine.version2.*;

import java.io.File;
import java.util.Arrays;

/**
 * @overview 
 *  A class to run KEngine version 2.0 and print suitable messages.
 * 
 * @author dmle
 */
public class EngineMain {
    
  /**
   * The main method
   * @effects
   *          initialise an Engine
   *          invoke tasks designated to an engine
   *          print suitable messages on the console
   */
  public static void main(String[] args) {
    /*
     * initialise an engine
     */
    System.out.println("KEngine version 2.0");
    Engine eng = new Engine();

    /*
     * perform tasks
     */

    // the test data directory
    String testDirUrl = "file://D:/testData";

    // add docs from the data directory
    System.out.println("Adding docs from URL: " + testDirUrl);
    try {
      eng.addDocs(testDirUrl);
    } catch (Exception e) {
      System.err.println("Error: " + e.getMessage());
      // e.printStackTrace();
    }

    // find doc
    String[] titles = {
        // invalid titles
        "Trường Đại học Hà nội",
        // valid titles
        "Hanoi University - Tin tức sinh viên",
        "Hanoi University - Tin tức &amp; Sự kiện",
        "Hanoi University - Tin tức &amp; Sự kiện (2) " // note the extra space at the end
    };

    Doc d;
    for (String title : titles) {
      System.out.println("? title = " + title);
      try {
        d = eng.findDoc(title);
        System.out.print("...found\n");
      } catch (Exception e) {
        System.out.print("...not found\n");
        //e.printStackTrace();
      }
    }

    // query: expect to throw exception here as this is not yet implemented
    String[] keys = { "sinh" };

    Query q;
    try {
      System.out.println("Querying using keywords: " + Arrays.toString(keys));
      q = eng.queryFirst(keys[0]);
    } catch (Exception e) {
      System.err.println("*Expected* error: " + e.getMessage());
    }
  }
}
